﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilLib;
using ServerBase.Service;
using ServerBase.DataBaseRedis;
using ServerGame.Manager;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using UtilPublic.Serialization;
using ServerBase.Other;
using static UtilPublic.Serialization.Jsh;
using static ServerBase.DataBaseRedis.RdsTxt;
using System.Threading;
using static UtilPublic.Redis.ServerRedis;
using static ServerBase.Config.Conf;
using ServerBase.Protocol;
using UtilPublic.ServerLoger;
using System.Text;
using ServerBase.Config;
using UtilPublic.Mysql;
using ServerPublic.Data;
using ServerPublic.Manager;
using ServerGame.GameObject;
using System.IO;
using UtilPublic.Timer;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using UtilPublic.Security;
using System.Collections.Specialized;
using UtilPublic.Http;
using System.Web;
using System.Xml.Linq;
using System.Xml;
using UtilPublic.ApiDispatcher;
using UtilPublic;
using ServerGame.Data;
using ServerBase.LunarSocket;
using Org.BouncyCastle.Crypto.Modes.Gcm;
using static ServerGame.Manager.Efun;

namespace ServerGame.Service
{
    public static class GameCmds
    {
        // 启动指令线程
        public static void TaskCmds()
        {
            try
            {
                ApiDpCmdMgr.AddController(BaseCmds.Instance);
                ApiDpCmdMgr.AddController(ApiDpCmdController.Instance);
            }
            catch (Exception ex)
            {
                loger.Error($"ApiDpCmd ApiDpCmdStart", ex);
            }

            ApiDpCmdMgr.Start();
        }
    }
    // 命令行指令
    public class ApiDpCmdController
    {
        #region 单例
        private static readonly ApiDpCmdController _instance = new ApiDpCmdController();
        public static ApiDpCmdController Instance { get { return _instance; } }
        private ApiDpCmdController() { } //私有构造方法(防止被外部实例化)
        #endregion

        [ApiDpCmd("saveall", "服务器存档")]
        public void 服务器存档(string cmd, string[] args)
        {
            ServerMgrGame.ServerSave(true);
        }
        [ApiDpCmd("game", "游戏服务器信息")]
        public void 游戏服务器信息(string cmd, string[] args)
        {
            Util.Show($"创建玩家数：{DataGame.ServerListPuid.Count}");
            Util.Show($"客户端链接：{BaseAppServer.SessionCount}");
            Util.Show($"缓存玩家数：{DataGame.DictPlayerCache.Count}");
            Util.Show($"活跃玩家数：{DataGame.DictPlayerOnline.Count}");
            Util.Show($"在线玩家数：{DataGame.DictPlayerOnline.Count}");
        }
        [ApiDpCmd("exit", "关闭服务器")]
        public void 关闭服务器(string cmd, string[] args)
        {
            ServerMgrGame.ServerExitPre();
        }
        [ApiDpCmd("save", "保存玩家档案")]
        public void 保存玩家(string cmd, string[] args)
        {
            if (args.Length < 2)
            {
                Util.Show("格式： save <玩家ID>");
                return;
            }
            long Puid = args[1].ToLong();
            if (!DataGame.GetPlayer(Puid, out var player, EGetPlayerSource.Online))
            {
                Util.Show($"这个玩家ID {Puid} 不在线");
                return;
            }
            player.Save();
        }
        [ApiDpCmd("dump", "打印")]
        public void Dump(string cmd, string[] args)
        {
            if (args.Length < 2)
            {
                Util.Show($"dump confglobal|bank1|bank2|saiji|king");
                return;
            }
            var type = args[1];
            string strid = "";
            if (args.Length > 2)
            {
                strid = args[2];
            }
            switch (type)
            {
                case "confglobal":
                    foreach (var kvp in ConfGlobal)
                    {
                        Util.Info($"{kvp.Key}\t\t\t{kvp.Value.SettBase.Value}");
                    }
                    break;
                default:
                    break;
            }
        }
        [ApiDpCmd("update", "刷新")]
        public void Update(string cmd, string[] args)
        {
            RoomMgr.RoomUpdate();
        }
        [ApiDpCmd("t", "测试")]
        public void Test(string cmd, string[] args)
        {
            loger.Debug($"测试打印开始");
            测试(true);
            loger.Debug($"测试打印结束");
        }
        public static void 测试(bool force = false)
        {
            if (true)
            {
                //loger.Debug($"{"aaa".ToInt()}");
            }
            //if (true)
            //{
            //    var c1 = new C1();
            //    //c1.ListTeam.Add(new BB1() { b = 3, });
            //    c1.ListTeam.Add(new AA1() { b = 3, a = 4, });
            //    var bb1 = c1.ListTeam[0];
            //    //var aa1 = bb1 as AA1;
            //    var aa1 = (AA1)bb1;
            //    aa1.a = 2;

            //    loger.Debug($"{((AA1)(c1.ListTeam[0])).a}");
            //}
            if (true)
            {
                //loger.Debug($"byte.MaxValue{byte.MaxValue}");
                //loger.Debug($"byte.MinValue{byte.MinValue}");

                //XXXX(EProtocolId.C2G_SHINE_MATCH, out C2G_Shine_Match ooo);
            }
            if (true)
            {
                //LoopServer.Dump();
                //LoopPlayer.Dump();

                //LunarObj SessionObj = null;

                //var bb = Util.Random.Next(100);

                //int b = 36;
                //int a = true switch
                //{
                //    _ when b < 0 => 1,
                //    _ when b < 10 => 2,
                //    _ when b < 30 => 3,
                //    _ when b < 60 => 4,
                //    _ => 5
                //};
                //loger.Debug($"{a}");

                //List<byte> l1 = new List<byte>() { 1, 2, 3 };
                //List<byte> l5 = new List<byte>() { 1, 2, 3, 4 };
                //var l2 = l1;
                //var l3 = new List<byte>(l1);
                //l1.Remove(3);
                //l2 = l5;
                //l4.Remove(2);
                //loger.Warn(11);
                //loger.Error(12);
                //loger.Fatal(13);
            }
            if (true)
            {
                //ResponseInfo response = HTTP.Send("http://127.0.0.1:9974/gm", "POST", new { Account = "pub", GtAction = "pub", GtArgs = "pub" }, new RequestOptions { ContentType = "application/json" });
                //string content = response.GetResponseContent();
                //loger.Warn(content);
            }
            if (true)
            {
                //loger.Debug($"IsMarkSilence {BaseServerInfo.AppServer.IsMarkSilence}");
                //loger.Debug($"FreeBufferObjHold {BaseServerInfo.AppServer.FreeBufferObjHold}");
                //loger.Debug($"FreeSocketObjHold {BaseServerInfo.AppServer.FreeSocketObjHold}");
                //loger.Debug($"FreeBufferObjPool {BaseServerInfo.AppServer.FreeBufferObjPool}");
                //loger.Debug($"FreeSocketObjPool {BaseServerInfo.AppServer.FreeSocketObjPool}");
                //loger.Debug($"Port {BaseServerInfo.AppServer.Port}");
                //loger.Debug($"WorkerThreadCount {BaseServerInfo.AppServer.WorkerThreadCount}");
                //loger.Debug($"MaxConnectionCount {BaseServerInfo.AppServer.MaxConnectionCount}");
                //loger.Debug($"SendPolicy {BaseServerInfo.AppServer.SendPolicy}");
                //loger.Debug($"Address {BaseServerInfo.AppServer.Address}");
                //loger.Debug($"Tag {BaseServerInfo.AppServer.Tag}");
                //loger.Debug($"FreeSocketObjLockTime {BaseServerInfo.AppServer.FreeSocketObjLockTime}");
                //loger.Debug($"OnSendSyncPolicy {BaseServerInfo.AppServer.OnSendSyncPolicy}");
                //loger.Debug($"ConnectionCount {BaseServerInfo.AppServer.ConnectionCount}");
                //loger.Debug($"HasStarted {BaseServerInfo.AppServer.HasStarted}");
                //loger.Debug($"State {BaseServerInfo.AppServer.State}");
                //loger.Debug($"IsSecure {BaseServerInfo.AppServer.IsSecure}");
                //loger.Debug($"Version {BaseServerInfo.AppServer.Version}");

                //loger.Debug($"0xFFFFFF {0xFFFFFF}");

                //loger.Debug($"1 {Convert.ToBoolean(1)}");
                //loger.Debug($"0 {Convert.ToBoolean(0)}");
                //loger.Debug($"2 {Convert.ToBoolean(2)}");
                //loger.Debug($"-1 {Convert.ToBoolean(-1)}");

                //loger.Debug($"{"1".ToBool()}");
                //loger.Debug($"{"0".ToBool()}");
                //loger.Debug($"{"2".ToBool()}");
                //loger.Debug($"{"-1".ToBool()}");
                //loger.Debug($"{"true".ToBool()}");
                //loger.Debug($"{"false".ToBool()}");
                //loger.Debug($"{"TRUE".ToBool()}");
                //loger.Debug($"{"FALSE".ToBool()}");
                //loger.Debug($"{"True".ToBool()}");
                //loger.Debug($"{"False".ToBool()}");
            }
            if (true)
            {
                //Dictionary<byte, CLS_ShineCell> DictMatrix = new Dictionary<byte, CLS_ShineCell>();
                //DictMatrix[1] = new CLS_ShineCell() { Type = ECellType.普通方块 };
                //var listHeroOpp = (from it in DictMatrix.Values
                //                   where (it.Type == ECellType.英雄方块)
                //                   select it);
                //for (int i = 0; i < 20; i++)
                //{
                //    loger.Debug($"aa {i} => {ShineMgr.Get连胜百分比(i)}");
                //}

                //var side1 = new CLS_ShineSide(); loger.Debug($"{Jstr(side1)}");
                //var battle1 = new CLS_ShineBattle(); loger.Debug($"{Jstr(battle1)}");
                //var side = new ShineSide(); loger.Debug($"{Jstr(side)}");
                //var battle = new ShineBattle(); loger.Debug($"{Jstr(battle)}");
            }
            if (true)
            {
                //ShineMgr.TestAwardWeek();
                //ShineMgr.TestAwardMonth();
                //for (int i = 0; i < 100; i++)
                //{
                //    test111(EShopType.秘法贤者, EShineRankBig.铜牌, 1);
                //}
            }
            if (true)
            {
                Dictionary<int, int> d1 = new Dictionary<int, int>();
                for (int i = 5; i < 20; i++)
                {
                    d1[i] = i;
                }
                var weight0 = new UtilRate<int>(d1);
            }
        }
        
    }
}